package org.mindrive.system.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.mindrive.system.collection.system.UserEntity;
import org.mindrive.system.core.MindriveException;
import org.mindrive.system.dao.*;
import org.mindrive.system.util.ConvertUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;

import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

public class BaseService extends BaseConstant {

    @Value("${file.path}")
    public String destPath;

    @Value("${three.member.management.enabled}")
    public boolean threeMemeberManagementEnabled;

    @Value("${mybatis-plus.global-config.db-config.columnFormat}")
    public String columnFormat;


    @Autowired
    public UploadDao uploadDao;


    @Autowired
    public UserDao userDao;

    @Autowired
    public PermissionDao permissionDao;

    @Autowired
    public PermissionPathDao permissionPathDao;

    @Autowired
    public RolePermissionDao rolePermissionDao;

    @Autowired
    public RoleDao roleDao;

    @Autowired
    public UserRoleDao userRoleDao;


    @Autowired
    public QuartzJobDao quartzJobDao;


    @Autowired
    public SystemInfoDao systemInfoDao;

    @Autowired
    public DictionaryDao dictionaryDao;

    @Autowired
    public DictionaryTypeDao dictionaryTypeDao;

    @Autowired
    public TransmissionDao transmissionDao;

    @Autowired
    public FileDao fileDao;

    @Autowired
    public ApprovalDao approvalDao;

    @Autowired
    public DetailDao detailDao;

    @Autowired
    public ScheduleDao scheduleDao;
    @Autowired
    public ScheduleDetailsDao scheduleDetailsDao;


    public String addEach(String source, List<String> targets) {
        List<String> sources = new ArrayList<String>();
        if (source != null && !"".equals(source)) {
            sources = ConvertUtil.split(source);
        }
        sources.addAll(targets);
        List<String> newTargets = sources.stream().distinct().collect(Collectors.toList());
        return String.join(DATA_SEPERATOR, newTargets);
    }

    public String deleteEach(String source, List<String> targets) {
        List<String> sources = new ArrayList<String>();
        if (source != null && !"".equals(source)) {
            sources = ConvertUtil.split(source);
        }
        sources.removeAll(targets);
        return String.join(DATA_SEPERATOR, sources);
    }



    public <T> Page<T> page(Class<T> entityClass, String pageSize, String currentPage) {
        Page<T> page = null;
        if (pageSize != null && currentPage != null && !"".equals(pageSize) && !"".equals(currentPage)) {
            long size = Long.valueOf(pageSize);
            long current = Long.valueOf(currentPage);
            page = new Page<T>(current, size);
        }
        return page;
    }


    public File getFileByMd5(String md5) {
        if (md5 == null || "".equals(md5)) {
            return null;
        }
        String filePath = destPath + File.separator + APP_FILES + File.separator + MERGE + File.separator + md5;
        File folder = new File(filePath);
        if (!folder.exists()) {
            throw new MindriveException("没有找到上传文件");
        }
        if (folder.listFiles().length == 0) {
            throw new MindriveException("没有找到上传文件");
        }
        File file = folder.listFiles()[0];
        return file;

    }

    public UserEntity getLoginUser() {
        Authentication auth = SecurityContextHolder.getContext().getAuthentication();
        String userId = auth.getName();
        QueryWrapper<UserEntity> query = new QueryWrapper<UserEntity>();
        query.eq("user_id", userId);
        UserEntity loginUser = userDao.getOne(query, false);
        return loginUser;
    }


    public int getPageCount(int total, int pageSize) {
        if (total % pageSize == 0) {
            return total / pageSize;
        }
        return total / pageSize + 1;
    }


    public String INDEX() {
        return String.format(columnFormat, "index");
    }

    public String NO() {
        return String.format(columnFormat, "no");
    }

    public String ORDER() {
        return String.format(columnFormat, "order");
    }

    public String CONDITION(){return String.format(columnFormat, "condition");}

}
